home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / Anwendun / Pov / POVTOOLS / ATSIS / ATSIS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  9.5 KB  |  320 lines

  1. /*************************************************************************
  2.                  ATsis -- create SIS out of depth information
  3.                  (C) 2/1995 by Christian Perle
  4.                  based on sirds program by W.A. Steer
  5.  *************************************************************************/
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifndef SEEK_SET
  10. #define    SEEK_SET    0        /* from beginning of file */
  11. #define    SEEK_CUR    1        /* from current location */
  12. #define    SEEK_END    2        /* from end of file */
  13. #endif
  14.  
  15. #define SI_NAME "sis.ppm"
  16.  
  17. /* depth of background in pixels */
  18. #define BKDEPTH -800
  19. /* eye separation in pixels */
  20. #define E 180
  21. /* observer-screen distance in pixels */
  22. #define O 700
  23. /* oversampling ratio (1, 2, 4 or 6) */
  24. #define OVERSAM 6
  25. /* slow hidden point removal on/off */
  26. #define DOHIDDENREM 0
  27. /* width of picture in pixels */
  28. #define XRES 640
  29. /* height of picture in pixels */
  30. #define YRES 470
  31. /* width of tile in pixels */
  32. #define TXR 96
  33. /* height of tile in pixels */
  34. #define TYR 470
  35.  
  36. unsigned char  thedepth[XRES*YRES];
  37. unsigned char  pixels[XRES*OVERSAM*3];
  38. unsigned char  thesird[XRES*(YRES+10)*3];  /* add 10 "help lines" */
  39. unsigned char  tile[TXR*TYR*3];
  40. int  z[XRES];
  41. int  link[XRES*OVERSAM];
  42.  
  43. FILE  *fd;
  44.  
  45. /* read depth file */
  46. read_depth(name)
  47. char  *name;
  48. {
  49.   int  x;
  50.  
  51.   fd = fopen(name, "rb");
  52.   if (fd == NULL) {
  53.     fprintf(stderr, "can't open %s\n", name);
  54.     exit(0);
  55.   }
  56.   for (x = 0; x < XRES*YRES; x++) thedepth[x] = fgetc(fd);
  57.   fclose(fd);
  58. }
  59.  
  60. /* read tile file */
  61. read_tile(name)
  62. char  *name;
  63. {
  64.   int  x;
  65.  
  66.   fd = fopen(name, "rb");
  67.   if (fd == NULL) {
  68.     fprintf(stderr, "can't open %s\n", name);
  69.     exit(0);
  70.   }
  71.   fseek(fd, 18L, SEEK_SET);  /* skip 18 header bytes */
  72.   for (x = 0; x < TXR*TYR*3; x++) tile[x] = fgetc(fd);
  73.   fclose(fd);
  74. }
  75.  
  76. /* add help marks */
  77. init_sis()
  78. {
  79.   int  x, y, th;
  80.   float  helpdist;
  81.  
  82.   /* clear thesird[] array */
  83.   for (x = 0; x < XRES*(YRES+10)*3; x++)
  84.     thesird[x] = 0;
  85.  
  86.   helpdist = abs(BKDEPTH)*E/(2.0*(abs(BKDEPTH)+O));
  87.   th = 0;
  88.   for (y = 9; y >= 0; y--) {
  89.     /* left mark */
  90.     for (x = XRES/2-helpdist-th; x <= XRES/2-helpdist+th; x++) {
  91.       thesird[3*(y*XRES+x)] = 255;  /* blue */
  92.       thesird[3*(y*XRES+x)+1] = 255;  /* green */
  93.       thesird[3*(y*XRES+x)+2] = 255;  /* red */
  94.     }
  95.     /* right mark */
  96.     for (x = XRES/2+helpdist-th; x <= XRES/2+helpdist+th; x++) {
  97.       thesird[3*(y*XRES+x)] = 255;  /* blue */
  98.       thesird[3*(y*XRES+x)+1] = 255;  /* green */
  99.       thesird[3*(y*XRES+x)+2] = 255;  /* red */
  100.     }
  101.     th++;
  102.   }
  103. }
  104.  
  105. /* render the SIS */
  106. do_sis()
  107. {
  108.   int  x, y, h, u, dx, xx, highest, separation, left, right, pp, tilep;
  109.   int  r, g, b, visible;
  110.   float  v;
  111.  
  112.   for (y = 0; y < YRES; y++) {
  113.     fprintf(stderr, "Rendering line %4d of %4d\r", y+1, YRES);
  114.     for (x = 0; x < XRES*OVERSAM; x++) link[x] = x;
  115.     highest = BKDEPTH;
  116.     for (x = 0; x < XRES; x++) {
  117.       h = BKDEPTH + thedepth[y*XRES+x];
  118.       z[x] = h;
  119.       if (h > highest) highest = h;
  120.     }
  121.     for (x = 0; x < XRES*OVERSAM; x++) {
  122.       separation = (E*OVERSAM*z[x/OVERSAM])/(z[x/OVERSAM]-O);
  123.       left = x-separation/2;
  124.       right = left+separation;
  125.       if ((left >= 0) && (right < XRES*OVERSAM)) {
  126.         visible = 1;
  127.         if (DOHIDDENREM == 1) {
  128.           v = 2.0*(O-z[x/OVERSAM])/E;
  129.           dx = 1;
  130.           do {
  131.             u = z[x/OVERSAM]+dx*v;
  132.             if ((z[(x+dx)/OVERSAM] >= u) || (z[(x-dx)/OVERSAM] >= u))
  133.               visible = 0;
  134.             dx++;
  135.           } while ((u <= highest) && (visible == 1));
  136.         }
  137.         if (visible == 1) link[right] = left;
  138.       }
  139.     }
  140.     for (x = 0; x < XRES*OVERSAM; x++) {
  141.       tilep = (y % TYR)*TXR + (x / OVERSAM) % TXR;
  142.       if (link[x] == x) {
  143.         pixels[3*x] = tile[3*tilep];
  144.         pixels[3*x+1] = tile[3*tilep+1];
  145.         pixels[3*x+2] = tile[3*tilep+2];
  146.       }
  147.       else {
  148.         pixels[3*x] = pixels[3*link[x]];
  149.         pixels[3*x+1] = pixels[3*link[x]+1];
  150.         pixels[3*x+2] = pixels[3*link[x]+2];
  151.       }
  152.     }
  153.     for (x = 0; x < XRES; x++) {
  154.       xx = x*OVERSAM;
  155.       switch (OVERSAM) {
  156.         case 1:
  157.           b = pixels[3*xx];
  158.           g = pixels[3*xx+1];
  159.           r = pixels[3*xx+2];
  160.           break;
  161.         case 2:
  162.           if ((x > 0) && (x < XRES-1)) {
  163.             b = pixels[3*xx]*42+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*24
  164.                 +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*5;
  165.             b = b/100;
  166.             g = pixels[3*xx+1]*42+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*24
  167.                 +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*5;
  168.             g = g/100;
  169.             r = pixels[3*xx+2]*42+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*24
  170.                 +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*5;
  171.             r = r/100;
  172.           }
  173.           else {
  174.             b = pixels[3*xx];
  175.             g = pixels[3*xx+1];
  176.             r = pixels[3*xx+2];
  177.           }
  178.           break;
  179.         case 4:
  180.           if ((x > 0) && (x < XRES-1)) {
  181.             b = pixels[3*xx]*26+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*18
  182.                 +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*12
  183.                 +(pixels[3*(xx-3)]+pixels[3*(xx+3)])*7;
  184.             b = b/100;
  185.             g = pixels[3*xx+1]*26+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*18
  186.                 +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*12
  187.                 +(pixels[3*(xx-3)+1]+pixels[3*(xx+3)+1])*7;
  188.             g = g/100;
  189.             r = pixels[3*xx+2]*26+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*18
  190.                 +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*12
  191.                 +(pixels[3*(xx-3)+2]+pixels[3*(xx+3)+2])*7;
  192.             r = r/100;
  193.           }
  194.           else {
  195.             b = pixels[3*xx];
  196.             g = pixels[3*xx+1];
  197.             r = pixels[3*xx+2];
  198.           }
  199.           break;
  200.         case 6:
  201.           if ((x > 0) && (x < XRES-1)) {
  202.             b = pixels[3*xx]*14+(pixels[3*(xx-1)]+pixels[3*(xx+1)])*14
  203.                 +(pixels[3*(xx-2)]+pixels[3*(xx+2)])*11
  204.                 +(pixels[3*(xx-3)]+pixels[3*(xx+3)])*8
  205.                 +(pixels[3*(xx-4)]+pixels[3*(xx+4)])*5
  206.                 +(pixels[3*(xx-5)]+pixels[3*(xx+5)])*3
  207.                 +(pixels[3*(xx-6)]+pixels[3*(xx+6)])*2;
  208.             b = b/100;
  209.             g = pixels[3*xx+1]*14+(pixels[3*(xx-1)+1]+pixels[3*(xx+1)+1])*14
  210.                 +(pixels[3*(xx-2)+1]+pixels[3*(xx+2)+1])*11
  211.                 +(pixels[3*(xx-3)+1]+pixels[3*(xx+3)+1])*8
  212.                 +(pixels[3*(xx-4)+1]+pixels[3*(xx+4)+1])*5
  213.                 +(pixels[3*(xx-5)+1]+pixels[3*(xx+5)+1])*3
  214.                 +(pixels[3*(xx-6)+1]+pixels[3*(xx+6)+1])*2;
  215.             g = g/100;
  216.             r = pixels[3*xx+2]*14+(pixels[3*(xx-1)+2]+pixels[3*(xx+1)+2])*14
  217.                 +(pixels[3*(xx-2)+2]+pixels[3*(xx+2)+2])*11
  218.                 +(pixels[3*(xx-3)+2]+pixels[3*(xx+3)+2])*8
  219.                 +(pixels[3*(xx-4)+2]+pixels[3*(xx+4)+2])*5
  220.                 +(pixels[3*(xx-5)+2]+pixels[3*(xx+5)+2])*3
  221.                 +(pixels[3*(xx-6)+2]+pixels[3*(xx+6)+2])*2;
  222.             r = r/100;
  223.           }
  224.           else {
  225.             b = pixels[3*xx];
  226.             g = pixels[3*xx+1];
  227.             r = pixels[3*xx+2];
  228.           }
  229.           break;
  230.       }
  231.       thesird[3*((y+10)*XRES+x)] = b;  /* blue */
  232.       thesird[3*((y+10)*XRES+x)+1] = g;  /* green */
  233.       thesird[3*((y+10)*XRES+x)+2] = r;  /* red */
  234.     }
  235.   }
  236. }
  237.  
  238. /* write SIS as TGA file */
  239. write_sis_tga()
  240. {
  241.   int  x;
  242.  
  243.   fd = fopen(SI_NAME, "wb");
  244.   fputc(0,fd);fputc(0,fd);fputc(2,fd);
  245.   fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);
  246.   fputc(0,fd);fputc(0,fd);fputc(0,fd);fputc(0,fd);
  247.   fputc(XRES % 256,fd);fputc(XRES / 256,fd);
  248.   fputc((YRES+10) % 256,fd);fputc((YRES+10) / 256,fd);
  249.   fputc(24,fd);fputc(32,fd);
  250.   for (x = 0; x < XRES*(YRES+10)*3; x++) fputc(thesird[x],fd);
  251.   fclose(fd);
  252. }
  253.  
  254. /* write SIS as PPM file */
  255. write_sis_ppm()
  256. {
  257.   int  x;
  258.  
  259.   fd = fopen(SI_NAME, "wb");
  260.   fprintf(fd, "P6\n");
  261.   fprintf(fd, "%d %d\n", XRES, YRES+10);
  262.   fprintf(fd, "255\n");
  263.   for (x = 0; x < XRES*(YRES+10); x++) {
  264.     fputc(thesird[3*x+2],fd);
  265.     fputc(thesird[3*x+1],fd);
  266.     fputc(thesird[3*x],fd);
  267.   }
  268.   fclose(fd);
  269. }
  270.  
  271. /* --------------------------------------------------------------------- */
  272.  
  273. main(argc, argv)
  274. int  argc;
  275. char  *argv[];
  276. {
  277.   int  argn;
  278.   char  *de_name, *ti_name;
  279.  
  280.   fprintf(stderr, "ATsis v1.0  (C) 2/1995 Christian Perle\n");
  281.   fprintf(stderr, "based on sirds program by W.A. Steer\n\n");
  282.   argn = 1;  /* number of arguments */
  283.   if (argn < argc) {
  284.     de_name = argv[argn];
  285.     argn++;
  286.     if (argn < argc) {
  287.       ti_name = argv[argn];
  288.       argn++;
  289.     }
  290.     else {  /* too few arguments */
  291.       fprintf(stderr, "usage: atsis depthfile tilefile\n");
  292.       exit(0);
  293.     }
  294.   }
  295.   else {  /* too few arguments */
  296.     fprintf(stderr, "usage: atsis depthfile tilefile\n");
  297.     exit(0);
  298.   }
  299.   if (argn != argc) {  /* too much arguments */
  300.     fprintf(stderr, "usage: atsis depthfile tilefile\n");
  301.     exit(0);
  302.   }
  303.   fprintf(stderr, "Rendering %s to ", de_name);
  304.   fprintf(stderr, SI_NAME);
  305.   fprintf(stderr, "\n");
  306.   fprintf(stderr, "Using tile file %s\n", ti_name);
  307.   fprintf(stderr, "Size %dx%d, ", XRES, YRES+10);
  308.   if (OVERSAM == 1) fprintf(stderr, "no oversampling");
  309.   else fprintf(stderr, "%d times oversampling", OVERSAM);
  310.   if (DOHIDDENREM == 1) fprintf(stderr, ", hidden point removal.\n\n");
  311.   else fprintf(stderr, ".\n\n");
  312.   read_depth(de_name);
  313.   read_tile(ti_name);
  314.   init_sis();
  315.   do_sis();
  316.   fprintf(stderr, "\n");
  317.   write_sis_ppm();
  318.   fprintf(stderr, "Done.\n");
  319. }
  320.